﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace FamilyTree
{
    public partial class TreeAdd : Form
    {
        public TreeAdd()
        {
            InitializeComponent();
        }

        private void label6_Click(object sender, EventArgs e)
        {
            string connString = @"Data Source = PC-20190806MQPW\SQLEXPRESS01; Initial Catalog = 终极电子族谱; User ID = sa; Password = 123456"; //数据库连接
            string sql = @"INSERT INTO tree VALUES('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "')";
            SqlConnection conn = new SqlConnection(connString);
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            try
            {
                cmd.ExecuteNonQuery();
                MessageBox.Show("信息已存入数据库！");
            }
            catch (Exception msg)
            {
                MessageBox.Show("出问题了！\n出错原因：" + msg.Message);
            }
            conn.Close();
        }

        private void label5_Click(object sender, EventArgs e)
        {
            string connString = @"Data Source = PC-20190806MQPW\SQLEXPRESS01; Initial Catalog = 终极电子族谱; User ID = sa; Password = 123456";
            string id = textBox1.Text;
            SqlConnection conn = new SqlConnection(connString);

            conn.Open();
            string sql = @"SELECT * FROM tree WHERE id='" + textBox1.Text + "'";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader dr = cmd.ExecuteReader();
            if (!dr.Read())
            {
                MessageBox.Show( "编号唯一，可创建");
                return;
            }
        }
    }
}
